﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;
using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string SK_D_Add(SK_D model, int ACID);


        [OperationContract]
        string SK_D_Update(SK_D model, int ACID);


        [OperationContract]
        string SK_DBydid_Delete(int did, int ACID);


        [OperationContract]
        SK_D SK_DBydid_Get(int did, int ACID);


        [OperationContract]
        List<SK_D> SK_D_Get(string sortBy, int ACID);




    }
    public partial class Service : IService
    {
        #region  添加SK_D表数据
        /// <summary>
        /// 添加SK_D
        /// </summary>
        /// <param name="model">SK_D实体</param>
        /// <returns>空或错误</returns>
        public string SK_D_Add(SK_D model, int ACID)
        {
            if (model.did == null)
            {
                return "『收款单从表ID』不能为空！";
            }
            if (model.jsje == null)
            {
                return "『结算金额』不能为空！";
            }
            if (model.blance == null)
            {
                return "『余额』不能为空！";
            }
            if (model.discount == null)
            {
                return "『折让金额』不能为空！";
            }
            if (model.ysje == null)
            {
                return "『应收金额』不能为空！";
            }
            if (model.Pdate == null)
            {
                return "『单据日期』不能为空！";
            }
            if (model.billtype == null)
            {
                return "『单据类型』不能为空！";
            }
            if (string.IsNullOrEmpty(model.billpid))
            {
                return "『单据号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『收款单号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.SK_D.Count(p => p.did == model.did) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.SK_D.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加SK_D表数据事物用重构
        /// <summary>
        /// 添加SK_D事物用重构
        /// </summary>
        /// <param name="model">SK_D实体</param>
        /// <returns>空或错误</returns>
        private string SK_D_Add(SK_D model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            //if (model.did == null)
            //{
            //    return "『收款单从表ID』不能为空！";
            //}
            if (model.jsje == null || model.jsje == 0)
            {
                return "『结算金额』不能为空！";
            }
            //if (model.blance == null)
            //{
            //    return "『余额』不能为空！";
            //}
            //if (model.discount == null)
            //{
            //    return "『折让金额』不能为空！";
            //}
            //if (model.ysje == null)
            //{
            //    return "『应收金额』不能为空！";
            //}
            if (model.Pdate == null)
            {
                return "『单据日期』不能为空！";
            }
            if (model.billtype == null)
            {
                return "『单据类型』不能为空！";
            }
            if (string.IsNullOrEmpty(model.billpid))
            {
                return "『单据号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『收款单号』不能为空！";
            }
            if (ctx.SK_D.Count(p => p.did == model.did) > 0)
            {
                return "主键已存在！";
            }
            ctx.SK_D.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  修改SK_D表数据
        /// <summary>
        /// 修改SK_D
        /// </summary>
        /// <param name="model">SK_D实体</param>
        /// <returns>空或错误</returns>
        public string SK_D_Update(SK_D model, int ACID)
        {
            if (model.did == null)
            {
                return "『收款单从表ID』不能为空！";
            }
            if (model.jsje == null)
            {
                return "『结算金额』不能为空！";
            }
            if (model.blance == null)
            {
                return "『余额』不能为空！";
            }
            if (model.discount == null)
            {
                return "『折让金额』不能为空！";
            }
            if (model.ysje == null)
            {
                return "『应收金额』不能为空！";
            }
            if (model.Pdate == null)
            {
                return "『单据日期』不能为空！";
            }
            if (model.billtype == null)
            {
                return "『单据类型』不能为空！";
            }
            if (string.IsNullOrEmpty(model.billpid))
            {
                return "『单据号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『收款单号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.SK_D.Count(p => p.did == model.did) != 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    SK_D SK_Dmodel = ctx.SK_D.Single(p => p.did == model.did);
                    if (model.did != null)
                    {
                        SK_Dmodel.did = model.did;
                    }
                    if (!string.IsNullOrEmpty(model.pid))
                    {
                        SK_Dmodel.pid = model.pid;
                    }
                    if (!string.IsNullOrEmpty(model.billpid))
                    {
                        SK_Dmodel.billpid = model.billpid;
                    }
                    if (model.billtype != null)
                    {
                        SK_Dmodel.billtype = model.billtype;
                    }
                    if (model.Pdate != null)
                    {
                        SK_Dmodel.Pdate = model.Pdate;
                    }
                    if (model.ysje != null)
                    {
                        SK_Dmodel.ysje = model.ysje;
                    }
                    if (model.discount != null)
                    {
                        SK_Dmodel.discount = model.discount;
                    }
                    if (model.blance != null)
                    {
                        SK_Dmodel.blance = model.blance;
                    }
                    if (model.jsje != null)
                    {
                        SK_Dmodel.jsje = model.jsje;
                    }
                    if (model.selected != null)
                    {
                        SK_Dmodel.selected = model.selected;
                    }
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion



        #region  删除SK_D表Bydid数据
        /// <summary>
        /// 删除SK_DBydid
        /// </summary>
        /// <param name="did">收款单从表ID</param>
        /// <returns>空或错误</returns>
        public string SK_DBydid_Delete(int did, int ACID)
        {
            if (did == null)
            {
                return "『收款单从表ID』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.SK_D.Count(p => p.did == did) != 1)
                    {
                        return null;
                    }
                    SK_D model = ctx.SK_D.Single(p => p.did == did);
                    ctx.SK_D.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  删除SK_D表Bydid数据[事物用重构]
        /// <summary>
        /// 删除SK_DBydid[事物用重构]
        /// </summary>
        /// <param name="did">收款单从表ID</param>
        /// <returns>空或错误</returns>
        private string SK_DBydid_Delete(int did, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (did == null)
            {
                return "『收款单从表ID』不能为空！";
            }
            if (ctx.SK_D.Count(p => p.did == did) != 1)
            {
                return null;
            }
            SK_D model = ctx.SK_D.Single(p => p.did == did);
            ctx.SK_D.DeleteOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  获取SK_D表Bydid数据
        /// <summary>
        /// 获取SK_DBydid
        /// </summary>
        /// <param name="did">收款单从表ID</param>
        /// <returns>当前表</returns>
        public SK_D SK_DBydid_Get(int did, int ACID)
        {
            if (did == null)
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.SK_D.Count(p => p.did == did) != 1)
                    {
                        return null;
                    }
                    return ctx.SK_D.SingleOrDefault(p => p.did == did);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion



        #region  获取SK_D表数据
        /// <summary>
        /// 获取SK_D
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<SK_D></returns>
        public List<SK_D> SK_D_Get(Expression<Func<SK_D, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.SK_D.ToList();
                        }
                        else
                        {
                            return ctx.SK_D.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.SK_D.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.SK_D.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取SK_D表数据
        /// <summary>
        /// 获取SK_D
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<SK_D></returns>
        public List<SK_D> SK_D_Get(string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return SK_D_Get(null, sortBy, ACID);
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取SK_D表数据计数
        /// <summary>
        /// 获取SK_D表数据计数
        /// </summary>
        /// <returns>List<SK_D></returns>
        public int SK_D_GetCount(Expression<Func<SK_D, bool>> predicate, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        return ctx.SK_D.Count();

                    }
                    else
                    {
                        return ctx.SK_D.Where(predicate).Count();
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion




        #region  获取SK_D分页表数据
        /// <summary>
        /// 获取SK_D分页表数据
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<SK_D></returns>
        public List<SK_D> SK_D_Getpage(int pageIndex, int pageSize, Expression<Func<SK_D, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.SK_D.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.SK_D.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.SK_D.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.SK_D.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  批量修改SK_D表数据
        /// <summary>
        /// 批量修改SK_D
        /// </summary>
        /// <param name="predicate">Expression<Func<SK_D, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<SK_D,SK_D>> 更新表达式树</param>
        /// <param name="returns">影响记录数</param>
        public int SK_D_batchUpdate(Expression<Func<SK_D, bool>> predicate, Expression<Func<SK_D, SK_D>> updater, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.SK_D.Update(predicate, updater);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量修改SK_D表数据事物用重构
        /// <summary>
        /// 批量修改SK_D事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<SK_D, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<SK_D,SK_D>> 更新表达式树</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int SK_D_batchUpdate(Expression<Func<SK_D, bool>> predicate, Expression<Func<SK_D, SK_D>> updater, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.SK_D.Update(predicate, updater);
            return ok;
        }
        #endregion


        #region  批量删除SK_D表数据
        /// <summary>
        /// 批量删除SK_D
        /// </summary>
        /// <param name="predicate">Expression<Func<SK_D, bool>></param> 查找表达式树
        /// <param name="returns">影响记录数</param>
        public int SK_D_batchDel(Expression<Func<SK_D, bool>> predicate, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.SK_D.Delete(predicate);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量删除SK_D表数据事物用重构
        /// <summary>
        /// 批量删除SK_D事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<SK_D, bool>></param> 查找表达式树
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int SK_D_batchDel(Expression<Func<SK_D, bool>> predicate, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.SK_D.Delete(predicate);
            return ok;
        }
        #endregion


    }
}

